import { http } from "@/utils/http";
import type { AjaxResult } from "./types";
import { baseUrlApi } from "./utils";

// 开始一次新销售
export const makeNewSale = () => {
  return http.request<AjaxResult>("get", baseUrlApi("/sale/makeNewSale"));
};

// 录入订单明细
export const enterItem = (params: EnterItemForm) => {
  return http.request<AjaxResult>("get", baseUrlApi("/sale/enterItem"), {
    params
  });
};

// 结束录入
export const endSale = () => {
  return http.request<AjaxResult>("get", baseUrlApi("/sale/endSale"));
};

// 确认支付

export const makePayment = (params: MakePaymentForm) => {
  return http.request<AjaxResult>("get", baseUrlApi("/sale/makePayment"), {
    params
  });
};

// 修改一行订单明细数量

export const changeQuantity = (data: SaleItem) => {
  return http.request<AjaxResult>("post", baseUrlApi("/sale/changeQuantity"), {
    data
  });
};

// 删除一行订单明细
export const deleteSaleItem = (itemSn: string) => {
  return http.request<AjaxResult>(
    "get",
    baseUrlApi("/sale/deleteSaleItem/" + itemSn)
  );
};

// 根据Id查询订单
export const getSaleById = (saleId: number) => {
  return http.request<AjaxResult>("get", baseUrlApi("/sale/" + saleId));
};

// 根据Id查询订单明细

export const listSaleItemById = (saleId: number) => {
  return http.request<AjaxResult>(
    "get",
    baseUrlApi("/sale/listSaleItemById/" + saleId)
  );
};

interface Sale {
  userName: string;
  saleNo: string;
  totalAmount: number;
  totalQuantity: number;
  status: string;
  remark: string;
}

interface SaleItem {
  index?: number;
  itemSn: string;
  productName: string;
  price: number;
  quantity: number;
}

interface EnterItemForm {
  itemSn: string;
  quantity: number;
}

interface MakePaymentForm {
  saleId?: number;
  payMethod: string;
  cashTendered: number;
}

interface QueryParams {
  pageNum: number;
  pageSize: number;
  saleId?: number;
  saleNo?: string;
  status?: string;
}

export type { Sale, SaleItem, EnterItemForm, MakePaymentForm, QueryParams };
